{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "quarterly-orange",
   "metadata": {},
   "source": [
    "# Problem 14.05 Required Power for R134a Compression Using a High Precision Equation of State"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intensive-arthritis",
   "metadata": {},
   "source": [
    "Refrigerant R134a is compressed from a saturated vapor at 5 °C to an outlet pressure of 1 MPa. Calculate the power of the compressor, using a high-precision EOS.\n",
    "\n",
    "The mechanical efficiency is 0.95, and the isentropic efficiency 0.7; the mass flow rate is 3000 kg/hr."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tired-superior",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is straightforward."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "comic-landing",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the conditions and imports\n",
    "from scipy.constants import bar, hour\n",
    "from thermo import ChemicalConstantsPackage, PRMIX, CEOSLiquid, CoolPropLiquid, CEOSGas, CoolPropGas, FlashPureVLS\n",
    "fluid = 'R134a'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = 5 + 273.15\n",
    "VF1 = 1\n",
    "P2 = 10*bar\n",
    "zs = [1]\n",
    "eta_isentropic = 0.7\n",
    "eta_mechanical = 0.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "derived-thanks",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The actual power is 28858 W\n",
      "The actual outlet temperature is  324.80 K\n"
     ]
    }
   ],
   "source": [
    "backend = 'HEOS'\n",
    "gas = CoolPropGas(backend, fluid, T=T1, P=1e5, zs=zs)\n",
    "liquid = CoolPropLiquid(backend, fluid, T=T1, P=1e5, zs=zs)\n",
    "\n",
    "flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "\n",
    "# Flash at inlet conditions to obtain initial enthalpy\n",
    "state_1 = flasher.flash(T=T1, VF=VF1)\n",
    "# Flash at outlet condition - entropy is conserved by compressors and expanders!\n",
    "state_2_ideal = flasher.flash(S=state_1.S(), P=P2)\n",
    "# Compute the change in enthalpy\n",
    "delta_H_ideal = (state_2_ideal.H()-state_1.H())\n",
    "# The definition of isentropic efficiency means that the actual amount of heat added is\n",
    "# dH_actual = dH_idea/eta_isentropic\n",
    "H_added_to_fluid_actual = delta_H_ideal/eta_isentropic\n",
    "\n",
    "state_2 = flasher.flash(H=state_1.H() + H_added_to_fluid_actual, P=P2)\n",
    "\n",
    "# To compute the actual power, itis more convinient to use the mass enthalpy\n",
    "actual_power_per_kg = (state_2.H_mass() - state_1.H_mass())/(eta_mechanical) # W/kg\n",
    "actual_power = actual_power_per_kg*3000/hour\n",
    "print(f'The actual power is {actual_power:.0f} W')\n",
    "print(f'The actual outlet temperature is {state_2.T: .2f} K')"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
